package org.springblade.modules.admin.service.parts.impl.strategy;

import lombok.extern.slf4j.Slf4j;
import org.springblade.common.constant.BaseRoleConstant;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.admin.entity.BusinessApprove;
import org.springblade.modules.admin.entity.MaintainMsg;
import org.springblade.modules.admin.enums.ApproveStatusEnum;
import org.springblade.modules.admin.enums.ApproveTypeEnum;
import org.springblade.modules.admin.enums.MsgTypeEnum;
import org.springblade.modules.admin.enums.TaskStatusEnum;
import org.springblade.modules.admin.service.MaintainMsgService;
import org.springblade.modules.admin.service.approve.strategy.ApproveBusinessFlow;
import org.springblade.modules.admin.service.approve.strategy.ApproveBusinessStrategy;
import org.springblade.modules.admin.service.parts.AbstractOrderStateStrategy;
import org.springblade.modules.admin.vo.BusinessApproveVO;
import org.springblade.modules.admin.vo.PartsSimpleInfoVO;
import org.springblade.modules.admin.vo.parts.PartsExecuteVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description 待分配策略实现 -> 对应状态4
 * @Author ysq
 * @Date 2024/05/16
 **/
@Slf4j
@Service(value = "allocationOrderStrategy")
public class AllocationOrderStrategyImpl extends AbstractOrderStateStrategy {

	@Autowired
	private MaintainMsgService msgService;

	@Override
	public Object handle(PartsExecuteVO executeParams) {
		BusinessApprove businessApprove = businessApproveService.getByWorkOrderId(executeParams.getPlanId());
		if (businessApprove == null) {
			log.error("审批流程不存在!, 工单id：[{}]", executeParams.getPlanId());
			return R.status(false);
		}
		// 更新审批 -> 审批通过
		BusinessApproveVO businessApproveVO = new BusinessApproveVO();
		businessApproveVO.setId(businessApprove.getId());
		businessApproveVO.setApproveStatus(ApproveStatusEnum.PASSED.getId());
		businessApproveVO.setRemark(executeParams.getRemark());
		ApproveBusinessFlow flowStrategy = ApproveBusinessStrategy.get(ApproveTypeEnum.ACCESSORY_APPLY.getId());
		boolean flowRes = flowStrategy.approve(businessApproveVO);
		if (!flowRes) {
			log.error("状态流转失败, 工单id：[{}]", executeParams.getPlanId());
			return R.status(false);
		}

		return super.handle(executeParams);
	}

	@Override
	public void after(PartsExecuteVO executeParams) {
		// 插入任务追踪日志
		maintainMalfunctionLogService.insertWorkLog(executeParams.getPlanId(),
			TaskStatusEnum.valueOf(executeParams.getEvent()).getName(),
			TaskStatusEnum.valueOf(executeParams.getEvent()).getStatus(),
			null,
			AuthUtil.getUserId(),
			"调度员");

		// 发送消息
		List<Long> userIds = userService.getUserByRoleName(BaseRoleConstant.DISPATCHERS);
		log.info("===========发送配件任务调度web消息,调度人id:[{}]===========", userIds);
		PartsSimpleInfoVO partsSimpleInfo = maintainPartsWorkService.getPartsSimpleInfoByPlanId(executeParams.getPlanId());
		MaintainMsg maintainMsg = new MaintainMsg();
		maintainMsg.setTitle(String.format("%s-%s", partsSimpleInfo.getBuildingName(), partsSimpleInfo.getElevatorAddress()));
		maintainMsg.setContent("您有一个【配件任务】需要调度");
		maintainMsg.setType(MsgTypeEnum.ELEVATOR_PARTS_DISPATCHERS.getId());
		maintainMsg.setOtherId(partsSimpleInfo.getId());
		msgService.sendMsg(maintainMsg, userIds);
	}
}
